<?php

namespace app\admin\service;

use app\admin\model\ExaminationCategory;
use app\admin\model\ExaminationPaper;
use app\admin\model\ExaminationQuestion;
use app\common\validate\CommonValidate;
use think\exception\ValidateException;
use think\facade\Request;

class ExaminationService
{
    public static function goEditCategory()
    {
        if (Request::isPost()) {
            $data = Request::post();
            //验证
            $rule = [
                'title|名称' => 'require',
                'thumb|图片' => 'require',
                'status|状态' => 'require',
            ];
            $message = [
                'title.require' => "分类名称不能为空",
                'thumb.require' => "分类图片不能为空",
                'status.require' => "分类状态不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            try {
                $data['title'] = str_replace(' ', '', trim($data['title']));
                if ($data['id']) {
                    $res = ExaminationCategory::update($data);
                } else {
                    unset($data['id']);
                    $exist = ExaminationCategory::where('title', $data['title'])->find();
                    if ($exist) return ['msg' => '记录存在', 'code' => 201];
                    $res = ExaminationCategory::create($data);
                }
                if ($res) return ['msg' => '操作成功', 'code' => 200];
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goCategoryStatus()
    {
        $data = Request::post();
        $model = ExaminationCategory::find($data['id']);
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->save([
                'status' => $data['status'],
            ]);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function goCategoryRemove()
    {
        $data = Request::post();
        $info = ExaminationCategory::find($data['id']);
        if ($info->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $info->delete();
//            return ['msg' => '删除功能只做演示', 'code' => 200];
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function goEdit()
    {
        if (Request::isPost()) {
            $data = Request::post();
            //验证
            $rule = [
                'title|考卷名称' => 'require',
                'cid|考卷分类' => 'require',
                'answer_time|答题时间' => 'require|min:1',
            ];
            $message = [
                'title.require' => "考卷名称不能为空",
                'cid.require' => "考卷分类不能为空",
                'answer_time.require' => "答题时间不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            $categoryInfo = ExaminationCategory::find($data['cid']);
            $data['thumb'] = $data['thumb'] ?? $categoryInfo['thumb'];
            try {
                $data['title'] = str_replace(' ', '', trim($data['title']));
                if ($data['id']) {
                    $res = ExaminationPaper::update($data);
                } else {
                    unset($data['id']);
                    $res = ExaminationPaper::create($data);
                }
                if ($res) return ['msg' => '操作成功', 'code' => 200];
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goRemove()
    {
        $data = Request::post();
        $info = ExaminationPaper::find($data['id']);
        if ($info->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $info->delete();
//            return ['msg' => '删除功能只做演示', 'code' => 200];
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function goEditQuestion()
    {
        if (Request::isPost()) {
            $data = Request::post();
            //验证
            $rule = [
                'qid|考题' => 'require|min:1',
                'pid|考卷' => 'require|min:1',
                'score|分数' => 'require|min:1',
            ];
            $message = [
                'qid.require' => "考题名称不能为空",
                'pid.require' => "参数错误",
                'score.require' => "分数不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            try {
                $exist = ExaminationQuestion::getInfoByQid($data['pid'], $data['qid'], $data['id'] ?? 0);
                if ($exist) return ['msg' => '该考题已存在考卷中', 'code' => 201];
                if ($data['id']) {
                    $res = ExaminationQuestion::update($data);
                } else {
                    unset($data['id']);
                    $res = ExaminationQuestion::create($data);
                }
                if ($res) return ['msg' => '操作成功', 'code' => 200];
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goRemoveQuestion()
    {
        $data = Request::post();
        $info = ExaminationQuestion::find($data['id']);
        if ($info->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $info->delete();
//            return ['msg' => '删除功能只做演示', 'code' => 200];
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }
}